<?php
load_ctl("admin_Common");
class Admin_ArticleController extends Admin_CommonController {
	function init(){
		parent::init();
		load_model('Class');
		$this->class = new APclass();
		load_model('Article');
		$this->article = new Article();
		load_model('Tag');
		$this->tag = new Tag();
	}
	public function indexAction(){
		$this->view->title = "文章信息";
		$this->view->article_lists = $this->article->get_article_lists();
		
		$page = isset($_GET["p"]) ? intval($_GET["p"]) : 1;
	    $page < 1 && $page = 1;
		$page_size = 10;
		
		$paginator = Zend_Paginator::factory($this->view->article_lists); 
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage($page_size); 
		$this->view->paginator = $paginator;
		$this->view->mainphp = 'views/scripts/admin/index/article.phtml';
		echo $this->view->render("index.phtml");
	}
	public function addarticleAction(){
		
		Zend_Loader::loadFile("fckeditor.php",'./api/fckeditor');
		$editor = new FCKeditor("art_content");
		$editor->BasePath   = $this->view->baseUrl.'/api/fckeditor/';
		$editor->ToolbarSet = 'Basic';
		$editor->Width      = '100%';
		$editor->Height     = '220';
		$editor->Value      = '';
		$this->view->fckeditor = $editor->Create();
		
		$this->view->action = 'add';
		$this->view->artclass_list = $this->class->get_artclass_list();
		$this->view->tag_list = $this->tag->get_tag_list();
		$this->view->mainphp = 'views/scripts/admin/index/doarticle.phtml';
		echo $this->view->render("index.phtml");
	}
	public function editarticleAction(){
		$this->view->action = 'edit';
		$id = $this->_request->get('id');
		$this->view->article_info = $this->article->get_article_info($id);
		
		Zend_Loader::loadFile("fckeditor.php",'./api/fckeditor');
		$editor = new FCKeditor("art_content");
		$editor->BasePath   = $this->view->baseUrl.'/api/fckeditor/';
		$editor->ToolbarSet = 'Basic';
		$editor->Width      = '100%';
		$editor->Height     = '220';
		$editor->Value      = htmlspecialchars_decode($this->view->article_info["art_content"],ENT_QUOTES);
		$this->view->fckeditor = $editor->Create();
		
		$this->view->artclass_list = $this->class->get_artclass_list();
		$this->view->tag_list = $this->tag->get_tag_list();
		$this->view->art_tag_list = $this->article->get_tagNames_by_artId($id);
		$this->view->tag_names = implode(',',$this->view->art_tag_list);
		$this->view->mainphp = 'views/scripts/admin/index/doarticle.phtml';
		echo $this->view->render("index.phtml");
	}
	function submitartAction(){
		if($this->_request->isPost()){
			$filter = new Zend_Filter_StripTags();
			$data = array(
				'art_title'=>$filter->filter($this->_request->getPost('art_title')),
				'art_keyword'=>$filter->filter($this->_request->getPost('art_keyword')),
				'art_content'=>htmlspecialchars($this->_request->getPost('art_content'),ENT_QUOTES,'utf-8'),
				'art_classid'=>$this->_request->getPost('art_classid'),
				'art_put_time'=>time(),
				'art_count'=>'0',
			);

			$tag_ids = array();
			if($filter->filter($this->_request->getPost('tag_names')) != ''){
				$tag_names = $filter->filter($this->_request->getPost('tag_names'));
				$tag_ids = $this->tag->add_tag($tag_names);
			}
			if($filter->filter($this->_request->getPost('tag_id')) != 0){
				$tag_ids[] = $filter->filter($this->_request->getPost('tag_id'));
			}
			$action = $this->_request->getPost('action');
			if ($action == 'edit') {	//修改
				$id = intval($filter->filter($this->_request->getPost('art_id')));
				$this->article->update_art_tag($tag_ids,$id);
				$this->article->update_article($id,$data);
				php_alert('修改文章成功');
			}else if($action == 'add') {//新增
				$last_art_id = $this->article->add_article($data);		
				$this->article->add_art_tag($tag_ids,$last_art_id);
				php_alert('添加文章成功');
			}
			$this->_redirect("admin/article");
		}else if(isset($_GET["action"]) && $_GET["action"] == 'del'){
			$filter = new Zend_Filter_StripTags();
			$id = intval($filter->filter($this->_request->get('id')));
			$this->article->del_article($id);
			php_alert('删除类别成功');
			$this->_redirect("admin/article");
		}
	}
}
?>